package pyip.lib.javalearn;

import java.io.UnsupportedEncodingException;

/**
 * getBytes()会使用平台默认编码, 不同平台默认编码不一致. 应该调用getBytes(charset)
 * @author yepeng
 * @date 2015年7月29日
 */
public class StringBytesDemo {
    public static void main(String[] args) {
        System.out.println("--英文---------------------");
        String str = "Wed May 20";
        System.out.println("content::" + str);
        System.out.println("length\t" + str.length());
        System.out.println("bytes\t" + str.getBytes().length);
        try {
            System.out.println("utf-8\t" + str.getBytes("utf-8").length);
            System.out.println("utf-32\t" + str.getBytes("utf-32").length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        System.out.println("--中文---------------------");

        String chinese = "你大爷";
        System.out.println("content::" + chinese);
        System.out.println("length\t" + chinese.length());
        System.out.println("bytes\t" + chinese.getBytes().length);
        try {
            System.out.println("utf-8\t" + chinese.getBytes("utf-8").length);
            System.out.println("utf-32\t" + chinese.getBytes("utf-32").length);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    }
}
